/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

import static com.googlecode.gchartjava.ArraysUtil.asUnmodifiableList;
import static java.util.Collections.unmodifiableList;

import java.util.LinkedList;
import java.util.List;

/**
 * Pie chart class. If the number of colors specified in the {@link Slice}
 * objects is less than the number of slices, then colors are interpolated. If
 * no colors are specified, pie segment colors are interpolated from dark orange
 * to pale yellow.
 * 
 * @see Slice
 * 
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class PieChart extends AbstractGraphChart {
	private static final String PIE_CHART_URL    = GCHART_URL + "?";
	private static final String CHART_TYPE_PARAM = "cht=";
	private static final String LABEL_PARAM      = "chl=";

	private boolean threeD = false;
	private final List<? extends Slice> slices;
	
	/**
	 * Create a pie chart with the given slices.
	 * 
	 * @param slices
	 *            the data slices of the pie chart.
	 *            
	 * @see Slice
	 */
	public PieChart(final Slice...slices){
		super();
		this.slices = asUnmodifiableList(slices);
	}
	
	/**
	 * Create a pie chart with the given slices.
	 * 
	 * @param slices
	 *            list of data slices of the pie chart.
	 *            
	 * @see Slice
	 */
	public PieChart(final List<? extends Slice> slices){
		this.slices = unmodifiableList(slices);
	}

	
	/**
	 * Is the chart a 3D Chart.
	 * 
	 * @return the threeD
	 */
	public boolean isThreeD() {
		return threeD;
	}

	/**
	 * If you want the chart to be 3D, set to true
	 * 
	 * @param threeD
	 *            boolean to determine if pie chart is rendered in 3D.
	 */
	public void setThreeD(final boolean threeD) {
		this.threeD = threeD;
	}
	
	/**
	 * Set the pie chart labels.
	 * 
	 * @param labels
	 *            list of pie chart labels.
	 */
	private void setLabels(final List<? extends String> labels)
	{
		String labelParam = "";
		int count = 0;
		for (String label : labels) {
			String l = label.replaceAll("\\s", "+");
			labelParam += (count++ == 0) ? l : "|" + l;
		}
		if(count > 0)
		  addParameter(LABEL_PARAM, labelParam);
	}
	
	/**
	 *
	 */
	@Override
	protected void prepareData() {
		final List<String>    labels  = new LinkedList<String>();
		final List<Color>     colors  = new LinkedList<Color>();
		
		final float[] d = new float[slices.size()];
		int i = 0;
		for (Slice slice : slices) {
			d[i++] = slice.getPercentage();
			labels.add(slice.getLabel());
			colors.add(slice.getColor());
		}
		//TODO: Might want to warning to user about % not adding up to 100.
		
		setData(asUnmodifiableList(new Data(d)));
		setLabels(unmodifiableList(labels));
		setColors(unmodifiableList(colors));
	}
	
	/**
	 *
	 */
	@Override
	protected String getBaseURLString() {
		return PIE_CHART_URL + CHART_TYPE_PARAM + ((threeD) ? "p3" : "p");
	}
}
